package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Activity;
import com.qk.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/activities")
@RestController
@Slf4j
public class ActivityController {

    @Autowired
    private ActivityService activityService;


    @GetMapping
    public Result list(Integer channel, Integer type, Integer status,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("列表查询:" + channel + " " + type + " " + status + " " + page + " " + pageSize);
        PageResult<Activity> pageResult = activityService.list(channel, type, status, page, pageSize);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result deleteByIds(@PathVariable Integer id){
        log.info("删除活动: {}", id);
        activityService.deleteByIds(id);
        return Result.success();
    }



    @PostMapping
    public Result add(@RequestBody Activity activity){
        log.info("新增活动: {}", activity);
        activityService.add(activity);
        return Result.success();
    }



    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询单个: {}", id);
        Activity activity = activityService.selectById(id);
        return Result.success(activity);
    }


    @PutMapping
    public Result update(@RequestBody Activity activity){
        log.info("更新活动: {}", activity);
        activityService.update(activity);
        return Result.success();
    }


    @GetMapping("/type/{type}")
    public Result getByType(@PathVariable Integer type){
        log.info("查询类型: {}", type);
        List<Activity> activities = activityService.getByType(type);
        return Result.success(activities);
    }

}
